package com.cookie.meetproject.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cookie.framework.base.BaseFragment;
import com.cookie.meetproject.R;
import com.cookie.meetproject.fragment.chat.AllFriendFragment;
import com.cookie.meetproject.fragment.chat.CallRecordFragment;
import com.cookie.meetproject.fragment.chat.ChatRecordFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.fragment
 * 文件名:    ChatFragment
 * 创建者:    cookie
 * 创建时间:   2020/1/5 0:42
 * 描述:      聊天
 */

public class ChatFragment extends BaseFragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private String[] title;
    private List<Fragment> fragmentList = new ArrayList<>();
    private ChatRecordFragment chatRecordFragment;
    private CallRecordFragment callRecordFragment;
    private AllFriendFragment allFriendFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, null);
        initView(view);
        return view;
    }

    private void initView(View view) {

        title = new String[]{getString(R.string.text_chat_tab_title_1), getString(R.string.text_chat_tab_title_2)
                , getString(R.string.text_chat_tab_title_3)};
        chatRecordFragment = new ChatRecordFragment();
        callRecordFragment = new CallRecordFragment();
        allFriendFragment = new AllFriendFragment();

        fragmentList.add(chatRecordFragment);
        fragmentList.add(callRecordFragment);
        fragmentList.add(allFriendFragment);

        tabLayout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.viewPager);

        for (int i = 0; i < title.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(title[i]));
        }
        viewPager.setOffscreenPageLimit(title.length);
        viewPager.setAdapter(new ChatPagerAdapter(getFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                defTabStyle(tab, 16);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //默认第一个选中
        defTabStyle(tabLayout.getTabAt(0), 16);
    }

    //设置tab的样式
    private void defTabStyle(TabLayout.Tab tab, int size) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_tab_text, null);
        TextView tv_tab = view.findViewById(R.id.tv_tab);
        tv_tab.setText(tab.getText());
        tv_tab.setTextColor(Color.WHITE);
        tv_tab.setTextSize(size);
        tab.setCustomView(tv_tab);
    }

    class ChatPagerAdapter extends FragmentStatePagerAdapter {


        public ChatPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            //super.destroyItem(container, position, object);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}

